<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="常数类别id" prop="typeId">
        <el-select v-model="form.typeId" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="常数项代码" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>

      <el-form-item label="常数项名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save()">保存数据</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "ConstantItemEdit",
  props: ["editid", "parentId"],
  data() {
    return {
      form: {
        id: this.editid,
        name: "",
        code: "",
      },
      options:[],
      isupdate: false,
      url: "/constant-item/add",
      rules: {
        name: [
          { required: true, message: "类别名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "类别名长度在1-100之间",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "编码不能为空", trigger: "blur" },
          { min: 1, max: 50, message: "编码长度在1-100之间", trigger: "blur" },
        ],
        sort: [
          { required: true, message: "编码不能为空", trigger: "blur" },
          { min: 1, max: 50, message: "编码长度在1-100之间", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    save() {
      //验证信息是否合法输入
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.axios.post(
            this.url,
            (response) => {
              this.$emit("update:show", false)
              this.$emit("getdata")
            },
            this.form
          )
        }
      })
    },
    getContantTypes()
    {
      this.axios.get(
        "/constant-type/list",
        (response) => {
          this.options = response.obj
          delete this.form["createtime"]
        },
        { withPage: 0 }
      )
    }
  },
  created() {
    if (this.editid) {
      this.isupdate = true;
      this.url = "/constant-item/update"
      this.axios.get(
        "/constant-item/getById",
        (response) => {
          this.form = response.obj
          delete this.form["createtime"]
        },
        { id: this.editid }
      )
    }
    this.getContantTypes()
  },

}
</script>

<style>
</style>